<?php
/**
 * Model de clientes.
 *
 * @author    Chavão <freela@chavao.net>
 * @copyright Copyright (c) 2011, Chavão
 * @version   0.1
 */
class Cliente extends AppModel {
	public function buscarClientes($psCriterio) {
		if(trim($psCriterio) != '')
			return $this->all(array("conditions" => array($psCriterio)));
		else
			return $this->all();
	}
	public function montarJSONGrafico() {
		$clie_data_fim = date('Y-m-d');
		$color =  array(1=>'#e7972a',2=>'#60c6ec',3=>'#91c544',4=>'#ce3d4b',5=>'#d2c62e',6=>'#b979d2',7=>'#2c60d2');
		$sql = "
		SELECT 
			p.plan_id,
			p.plan_nome,
			i.total 
		FROM 
			ldct_plano p 
		LEFT JOIN (
			SELECT 
				plan_id,
				count(*) as total 
			FROM 
				ldct_cliente 
			WHERE 
				clie_status = 1
			-- AND clie_data_fim > '".$clie_data_fim."'
			GROUP BY 
				plan_id
			) i 
		ON 
			i.plan_id = p.plan_id
		";
		$lines = $this->fetch($sql);
		$total_clientes = 0;
		foreach($lines as $i=>$line) {
			$lines[$i]['total'] = ($lines[$i]['total']) ? $lines[$i]['total'] : 0;
			$total_clientes = $total_clientes + $lines[$i]['total'];
		}
		foreach($lines as $i=>$line)
			@$lines[$i]['percent'] = number_format((($lines[$i]['total']*100)/$total_clientes),0,'','');
		$i = 1;
		foreach($lines as $line) {
			$line['total'] = ($line['total']) ? $line['total'] : 0;
			$pos[] = "{ name: '".$line['plan_nome']."', y: ".$line['percent'].", color: '".$color[$i]."' }";
			$i++;
			if($i == 7)
				$i = 1;
		}
		$return = implode(',',$pos);
		return $return;
	}
}